package com.zzk.lanouhn.myapplication1.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.intefaces.FindDetailsCaiDanOnItemClicklistener;
import com.zzk.lanouhn.myapplication1.modles.FindDetailsModle;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/9/9.
 */
public class FindDetailsCaiDanAdapter extends RecyclerView.Adapter<FindDetailsCaiDanAdapter.MyHolder> {

    private Context context;
    private FindDetailsModle modle;
    private List<FindDetailsModle.TripDaysBean.NodesBean> nodesBean = new ArrayList<>();

    private FindDetailsCaiDanOnItemClicklistener findDeailsOnItemClick;

    public void setMyOnItemClick(FindDetailsCaiDanOnItemClicklistener onItem) {
        this.findDeailsOnItemClick = onItem;
    }

    public FindDetailsCaiDanAdapter(Context context, FindDetailsModle modle) {
        this.context = context;
        this.modle = modle;

        for (int i = 0; i < modle.getTrip_days().size(); i++) {
            int day = modle.getTrip_days().get(i).getDay();
            List<FindDetailsModle.TripDaysBean.NodesBean> btemps = modle.getTrip_days().get(i).getNodes();
            for (FindDetailsModle.TripDaysBean.NodesBean b : btemps) {
                b.setTripDay(day);
            }

            nodesBean.addAll(modle.getTrip_days().get(i).getNodes());
        }
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_find_details_candan, null);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyHolder holder, int position) {


        if (nodesBean.get(position).getEntry_name() == null) {
            holder.ll_day.setVisibility(View.VISIBLE);
            holder.ll_weizhi.setVisibility(View.GONE);

            holder.tv_day.setText("DAY" + nodesBean.get(position).getTripDay());

        } else {
            holder.ll_weizhi.setVisibility(View.VISIBLE);
            holder.ll_day.setVisibility(View.GONE);

            holder.tv_weizhi.setText(nodesBean.get(position).getEntry_name() + "");
        }

        if (findDeailsOnItemClick != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    findDeailsOnItemClick.myOntemClick(pos);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        int size = 0;
        for (int i = 0; i < modle.getTrip_days().size(); i++) {
            size += modle.getTrip_days().get(i).getNodes().size();
        }
        return size;
    }

    public class MyHolder extends RecyclerView.ViewHolder {
        private LinearLayout ll_day, ll_weizhi;
        private TextView tv_day, tv_weizhi;

        public MyHolder(View itemView) {
            super(itemView);
            ll_day = (LinearLayout) itemView.findViewById(R.id.ll_find_details_caidan_day);
            ll_weizhi = (LinearLayout) itemView.findViewById(R.id.ll_find_details_caidan_weizhi);
            tv_day = (TextView) itemView.findViewById(R.id.tv_caidan_day);
            tv_weizhi = (TextView) itemView.findViewById(R.id.tv_caidan_weizhi);
        }
    }
}
